package tetris;
import java.util.Random;

public class Shape {
    // <editor-fold defaultstate="collapsed" desc="enum Tetrominoes{}">
    enum Tetrominoes {

        NoShape, ZShape, SShape, LineShape,
        TShape, SquareShape, LShape, MirroredLShape,BongMaShape
    };// </editor-fold>
    public Tetrominoes pieceShape;
    public int coords[][];
    public int shapeOrder;
    private int[][][] coordsTable;
    // <editor-fold defaultstate="collapsed" desc="Shape()">
    public Shape() {

        coords = new int[4][2];
        setShape(Tetrominoes.NoShape);

    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="setShape(Tetrominoes shape)">
    public void setShape(Tetrominoes shape) {

        coordsTable = new int[][][]{
            {{0, 0}, {0, 0}, {0, 0}, {0, 0}},
            {{0, -1}, {0, 0}, {-1, 0}, {-1, 1}},
            {{0, -1}, {0, 0}, {1, 0}, {1, 1}},
            {{0, -1}, {0, 0}, {0, 1}, {0, 2}},
            {{-1, 0}, {0, 0}, {1, 0}, {0, 1}},
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
            {{-1, -1}, {0, -1}, {0, 0}, {0, 1}},
            {{1, -1}, {0, -1}, {0, 0}, {0, 1}},
            {{0, 0}, {0, 0}, {0, 0}, {0, 0}}
        };

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 2; ++j) {
                coords[i][j] = coordsTable[shape.ordinal()][i][j];
            }
        }
        pieceShape = shape;
        

    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="setX(int index, int x)">
    private void setX(int index, int x) {
        coords[index][0] = x;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="setY(int index, int y)">
    private void setY(int index, int y) {
        coords[index][1] = y;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="x(int index) ">
    public int x(int index) {
        return coords[index][0];
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="y(int index)">
    public int y(int index) {
        return coords[index][1];
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="getShape()">
    public Tetrominoes getShape() {
        return pieceShape;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="setRandomShape()">
    public void setRandomShape() {
        Random r = new Random();
        int x = Math.abs(r.nextInt()) % 7 + 1;
        shapeOrder = x;
        Tetrominoes[] values = Tetrominoes.values();
        setShape(values[x]);
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="minX()">
    public int minX() {
        int m = coords[0][0];
        for (int i = 0; i < 4; i++) {
            m = Math.min(m, coords[i][0]);
        }
        return m;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="minY()">
    public int minY() {
        int m = coords[0][1];
        for (int i = 0; i < 4; i++) {
            m = Math.min(m, coords[i][1]);
        }
        return m;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Shape rotateLeft()">
    public Shape rotateLeft() {
        if (pieceShape == Tetrominoes.SquareShape) {
            return this;
        }

        Shape result = new Shape();
        result.pieceShape = pieceShape;

        for (int i = 0; i < 4; ++i) {
            result.setX(i, y(i));
            result.setY(i, -x(i));
        }
        return result;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="rotateRight()">
    public Shape rotateRight() {
        if (pieceShape == Tetrominoes.SquareShape) {
            return this;
        }

        Shape result = new Shape();
        result.pieceShape = pieceShape;

        for (int i = 0; i < 4; ++i) {
            result.setX(i, -y(i));
            result.setY(i, x(i));
        }
        return result;
    }// </editor-fold>
}